import { defineStore } from 'pinia'
import { getUserInfo, getUserList, editUser, deleteUser } from '@/api/user/index'
import type { UserInfo } from '@/stores/interface'

export const userInfoStore = defineStore('userInfo', {
    state: (): UserInfo => {
        return {
            userId: '',
            userName: '',
            nickName: '',
            userType: '',
            email: '',
            phonenumber: '',
            roleId: '',
            sex: '',
            remark: '',
            status: 0
        }
    },
    actions: {
        async getUserInfo(params) {
            try {
                const data = await getUserInfo(params)
                if (data.state === 'OK' && data.body) {
                    this.$state = data.body
                }
                return Promise.resolve(data)
              } catch(error) {
                return Promise.reject(error)
            }
        },
        async getUserList(params) {
            try {
                const data = await getUserList(params)
                return Promise.resolve(data)
              } catch(error) {
                return Promise.reject(error)
            }
        },
        async editUser(params) {
            try {
                const data = await editUser(params)
                return Promise.resolve(data)
              } catch(error) {
                return Promise.reject(error)
            }
        },
        async deleteUser(params) {
            try {
                const data = await deleteUser(params)
                return Promise.resolve(data)
              } catch(error) {
                return Promise.reject(error)
            }
        },
    }
})
